/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.race.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.race.dao.RaceScheduleTeamDao;
import com.lczyfz.istep.modules.race.dao.RaceScheduleTeamSubjectDao;
import com.lczyfz.istep.modules.race.entity.*;
import com.lczyfz.istep.modules.race.service.*;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSubject;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSubjectType;
import com.lczyfz.istep.modules.resource.service.ResourceCtfSubjectService;
import com.lczyfz.istep.modules.resource.service.ResourceCtfSubjectTypeService;
import com.lczyfz.istep.modules.sys.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 竞赛题库管理Controller
 * @author CY
 * @version 2017-04-24
 */
@Controller
@RequestMapping(value = "${frontPath}/race/present")
public class RacePresentController extends BaseController {
  @Autowired
   private RaceScheduleService raceScheduleService;

	private  RaceLoginController raceLoginController;

	@Autowired
	private  RaceRepertoryService raceRepertoryService;

   @Autowired
    private  RaceScheduleTeamSubjectDao raceScheduleTeamSubjectDao;

	@Autowired
	RaceTeamScoreService raceTeamScoreService;

    static public  RaceSchedule   raceScheduleStatic = null;

	@RequestMapping(value = "test3")
	public String test3(Model model) {
         return "modules/race/test3";
	}

	@RequestMapping(value = "")
	public String present(Model model) {

		//获取开启的赛程
		RaceSchedule raceSchedule = raceScheduleService.scheduleStatus();
//		if (null==raceSchedule){
//			return "modules/race/front/index";
//		}

		model.addAttribute("raceSchedule", raceSchedule);
		model.addAttribute("time", raceScheduleService.timeValue);

	//提交答对的前十条信息
		if (raceLoginController.getFlags() != null && raceLoginController.getFlags().size() != 0) {
			List<String> flags = new ArrayList<String>();
			for (int i = raceLoginController.getFlags().size()-1; i >= 0; i--){
				flags.add(raceLoginController.getFlags().get(i));
			}
			model.addAttribute("flags", flags);
		}

		//团队排行
		List<RaceScheduleTeamSubject> raceScheduleTeamSubjects = raceScheduleTeamSubjectDao.teamTotalScoreRanking();
		model.addAttribute("raceScheduleTeamSubjects", raceScheduleTeamSubjects);
		List<List<Integer>> teamlist = new ArrayList<>();
		List<String> teamNames = new ArrayList<>();
		int count = 10;


        //统计图记录了多少个时间点
		int num = 0;
		for (RaceScheduleTeamSubject raceScheduleTeamSubject : raceScheduleTeamSubjects) {
			List<RaceTeamScore> raceTeam = raceTeamScoreService.findTeam(raceScheduleTeamSubject.getTeamid(), raceScheduleTeamSubject.getScheduleid());
			if (raceTeam.size()> num){
				num = raceTeam.size();
			}
		}

		//拿到所有的参赛团队
		for (RaceScheduleTeamSubject raceScheduleTeamSubject : raceScheduleTeamSubjects) {
			List<Integer> scorelist = new ArrayList<>(12);
			List<RaceTeamScore> raceTeam = raceTeamScoreService.findTeam(raceScheduleTeamSubject.getTeamid(),raceScheduleTeamSubject.getScheduleid());
		    	int subtraction = num - raceTeam.size();
					if (subtraction != 0){
						for (int j = 0; j < subtraction; j++){
							scorelist.add(0);
						}
					}
				for (RaceTeamScore raceTeamScore : raceTeam) {
				     scorelist.add(Integer.parseInt(raceTeamScore.getTotalScore()));
				}
			teamlist.add(scorelist);
			teamNames.add("\"" + raceScheduleTeamSubject.getTeamid() + "\"");
		}
		model.addAttribute("teamlist", teamlist);
		model.addAttribute("teamNames", teamNames);

		if (raceSchedule != null) {
			raceScheduleStatic = raceSchedule;
		}
          if (raceScheduleStatic != null) {
			  RaceRepertory raceRepertory = raceRepertoryService.findRepertoryName(raceScheduleStatic.getRepertoryId());
			  List<RaceCtfSubject> raceCtfSubjects = raceScheduleService.findRaceSubject(raceRepertory.getId());
			  model.addAttribute("raceCtfSubjects", raceCtfSubjects);
		  }
		     List<RaceScheduleTeamSubject> raceScheduleTeamSubjectList =  raceScheduleTeamSubjectDao.findList(new RaceScheduleTeamSubject());
	     	model.addAttribute("raceScheduleTeamSubjectList", raceScheduleTeamSubjectList);



		//展示粒度
		List<String> displaySizeList = new ArrayList<>();
		System.out.println("-----进入此方法"+raceSchedule);
		if (raceSchedule!=null) {
			if (Integer.parseInt(raceSchedule.getDisplaySize()) >= 60) {

				for (int i = num; i > 0; i--){
					if (i == 1){
						displaySizeList.add("\"" + "当前" + "\"");
					}else {
						displaySizeList.add("\"" + " "+  (i-1) +"个小时前" + "\"");
					}
				}

			} else if (Integer.parseInt(raceSchedule.getDisplaySize()) == 5) {
				for (int i = num; i > 0; i--){
					if (i == 1){
						displaySizeList.add("\"" + "当前" + "\"");
					}else {
						displaySizeList.add("\"" + " "+  ((i-1)*5) +"分钟前" + "\"");
					}
				}
			}
			if (Integer.parseInt(raceSchedule.getDisplaySize()) == 10) {

				for (int i = num; i > 0; i--){
					if (i == 1){
						displaySizeList.add("\"" + "当前" + "\"");
					}else {
						displaySizeList.add("\"" + " "+  ((i-1)*10) +"分钟前" + "\"");
					}
				}
			}
			if (Integer.parseInt(raceSchedule.getDisplaySize()) == 20) {
				for (int i = num; i > 0; i--){
					if (i == 1){
						displaySizeList.add("\"" + "当前" + "\"");
					}else {
						displaySizeList.add("\"" + " "+  ((i-1)*20) +"分钟前" + "\"");
					}
				}
			}
			if (Integer.parseInt(raceSchedule.getDisplaySize()) == 30) {
				for (int i = num; i > 0; i--){
					if (i == 1){
						displaySizeList.add("\"" + "当前" + "\"");
					}else {
						displaySizeList.add("\"" + " "+  ((i-1)*30) +"分钟前" + "\"");
					}
				}
			}
//		    displaySizeList.add("\"" + "当前" + "\"");

		model.addAttribute("displaySizeList", displaySizeList);
		//todo:替换成后台设置的数值
		int refreshSize = Integer.parseInt(raceSchedule.getSwitchTime());
		model.addAttribute("refreshSize", refreshSize);
		}

		return "modules/race/front/index";
	}

}